package com.quectel.constant.global;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2022/01/17 11:17
 */
public class SqlConstants {


    public static final String LIMIT = "limit";
    public static final String OFFSET = "offset";
    /**
     * 时间格式化字段参数key
     */
    public static final String DATE_FORMAT_TYPE_FILED = "format";

    public static final String MYSQL_LIMIT_ONE = "limit 1";

    @AllArgsConstructor
    public enum MysqlDateTimeFormatTypeEnum {
        /**
         * 小时
         */
        HOUR("%Y-%m-%d %H:00:00"),
        /**
         * 天
         */
        DAY("%Y-%m-%d"),
        /**
         * 月
         */
        MONTH("%Y-%m");
        @Getter
        private String format;
    }

    public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
        Map<Object, Boolean> map = new ConcurrentHashMap<>();
        return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }

}
